/*
Copyright (C) 2010 
Author		  Oliver Schenk
Contact		  Oliver.Schenk76@gmail.com

This program is free software; you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; 
if not, see <http://www.gnu.org/licenses/>.
*/
package SelfBookingSystem;
import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import SelfBookingSystem.MyFramework.ContactAPI;



public class AddUserInformation extends HttpServlet {

	private String AppID;
	private String FirstName;	
	private String LastName;	
	private String Telephone;	
	private String Email;
	
	private String ConfigValueEmail;
	private String ConfigValuePassword;
	
	
	private static final long serialVersionUID = 1L;
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {		

			AppID = req.getParameter("AppID");
			FirstName = req.getParameter("FirstName");
			LastName = req.getParameter("LastName");
			Telephone = req.getParameter("Telephone");
			Email = req.getParameter("Email");
			
			System.out.println("AddUserInformation - AppID: " + AppID);
			System.out.println("AddUserInformation - FirstName: " + FirstName);
			System.out.println("AddUserInformation - LastName: " + LastName);
			System.out.println("AddUserInformation - Telephone: " + Telephone);
			System.out.println("AddUserInformation - Email: " + Email);
			
			//Get Configuration Data
			//Call Private Function
			GetConfigurationData();
			
			System.out.println("AddUserInformation - ConfigValueEmail: " + ConfigValueEmail);
			System.out.println("AddUserInformation - ConfigValuePassword: " + ConfigValuePassword);
			
			
			int result = PutUserInformationInGoogleContacts();
			
			
			resp.sendRedirect("/Admin/index.jsp?Result="+ result +"&AddUser=true&AppID=" + AppID + "&GoodCredentials=true");
			
	}
	
	private void GetConfigurationData(){
		//Get Configuration Data
		Map<String, String> m = SelfBookingSystem.GetConfigurationValues.GetConfigurationValues(AppID);
		ConfigValueEmail = m.get("EmailAddress");
		ConfigValuePassword = m.get("Password");
	}
	
	
	private int PutUserInformationInGoogleContacts(){
		int result=0;
		String ContactID = new String();
		ContactAPI addContact = new ContactAPI();
		result = addContact.GetConnection(ConfigValueEmail, ConfigValuePassword);
		//If UserCredentials are not correct return
		if (result==9999){
			return result;  
		}else{
			addContact.setFullName(FirstName, LastName);
			addContact.setPrimaryPhoneNumber(Telephone);
			addContact.setPrimaryEmail(Email);
			addContact.AddUserToGroup("SelfBookingApplication");
			ContactID = addContact.InsertContact();
			System.out.println("AddUserInformation - ContactID: " + ContactID);
		}
		return result;
	}
	
}
